<?php

use App\Models\ChatGroup\ChatGroupSessionModel;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Jialeo\LaravelSchemaExtend\Schema;

class CreateChatGroupSessionTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('chat_group_session', function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('groupId')->default(0)->comment('群聊id');
            $table->unsignedInteger('shopId')->default(0)->comment('门店id');
            $table->unsignedInteger('isNew')->default(0)->comment('是否最新会话 0否 1是');
            $table->unsignedInteger('lastMsgTime')->default(0)->comment('最后一条消息时间');
            $table->tinyInteger('robotIn')->default(0)->comment('机器人介入状态 0否 1是');
            $table->tinyInteger('serveIn')->default(0)->comment('客服介入状态 0无 1介入 2呼叫中');
            $table->unsignedInteger('serveInTime')->default(0)->comment('呼叫时间');
            $table->unsignedInteger('serveUid')->default(0)->comment('客服uid');
            $table->string('serveName', 32)->default('')->comment('客服昵称');
            $table->unsignedInteger('status')->default(0)->comment('会话状态 0关闭 1正常');

            $table->unsignedInteger('createdAt')->default(0)->comment('创建时间');
            $table->unsignedInteger('updatedAt')->default(0)->comment('修改时间');
            $table->collation = 'utf8mb4_general_ci';
            $table->comment   = '群聊 - 会话表';

            $table->index(['groupId'], 'idx_groupId');
            $table->index(['createdAt'], 'idx_createdAt');
            $table->index(['shopId'], 'idx_shopId');
            $table->index(['serveInTime'], 'idx_serveInTime');
        });

        $id  = ChatGroupSessionModel::query()->insertGetId(['shopId' => 0, 'groupId' => 0, 'createdAt' => 0]);
        ChatGroupSessionModel::query()->where('id', $id)->update(['id' => 0]);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('chat_group_session');
    }
}
